﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Net.Sockets;

namespace TCPInvoice
{
    public partial class Route : Form
    {
        public Route()
        {
            InitializeComponent();
        }

        private Invoice invoice;
        private void RouteClosed(object sender, FormClosingEventArgs e)
        {
            notifyIcon1.Visible = false;
            invoice.CloseConnection();
            Application.Exit();
        }

        private void RouteShown(object sender, EventArgs e)
        {
            invoice = new Invoice(this.logTextBox);
            if (invoice.OpenConnection() >= 0)
            {
                logTextBox.AppendText("Se estableció la conexión con la Impresora Fiscal." + Environment.NewLine);
                bgWorkerTCPListen.RunWorkerAsync();
                notifyIcon1.ShowBalloonTip(1);
            } else
            {
                MessageBox.Show("No se pudo establecer conexión con la Impresora Fiscal", "Error de conexión",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                Close();
            }
        }
        private void BgWorkerTCPListenerDoWork(object sender, DoWorkEventArgs e)
        {
            invoice.StartListening();
        }

        private void ClearLogBtnClick(object sender, EventArgs e)
        {
            logTextBox.Text = String.Empty;
        }

        private void NotifyIcon1Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Normal;
            TopMost = true;
            Focus();
            BringToFront();
            TopMost = false;
        }
    }
}
